﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using sscore;
using DM4;

namespace DMN_MailListener
{
    public delegate void WorkerResultHandler(WorkerResult wr);
    public delegate OperationResult LogHandler(string aMessage, MessageType type = MessageType.Info);

    internal delegate void MailListenerResultHandler(TaskParams tprm);
    public delegate void MailListenHandler(List<MailListenerWorkerParameters> mailParams);

    public class WorkerResult
    {
        public int CompleteCount;
    }

    internal class TaskParams
    {
        public DateTime Id = DateTime.Now;
        public CancellationToken Ct;
        public LogHandler Log;

        public MailChecker MailChecker;
        public MailListenerWorkerParameters MailParams;
        public string ConnectionString;
    }

    public class CreateMailParamsTaskParams
    {
        public CancellationToken Ct;

        public List<MailListenerParameters> MlParams;
        public MailListenHandler MailCheck;
    }

    public class MailLink
    {
        public int Id = DM4Consts.DefaultNULLId;
        public DateTime LetterCheckResultId;
        public string Link = "";
        public string User = "";
        public string Password = "";
        public string Status = DM4Consts.SessionPrepare;
    }

    public class MailListenerWorkerParameters
    {        
        public MailAccount MailAccountForCheck;
        /// <summary>
        /// список рабочих параметров для проверки каждого из писем
        /// </summary>
        public List<LetterWorkParameters> LettersWorkParam = new List<LetterWorkParameters>();
    }

    /// <summary>
    /// список рабочих параметров для проверки одного письма
    /// </summary>
    public class LetterWorkParameters
    {
        public DateTime StartTime = DateTime.Now;
        public MailSender Sender;
        public Letter Letter;
        public List<MailRuleResult> MailRules = new List<MailRuleResult>();

        public int Timeout = MailSenderConsts.Timeout;
        //public int AttemptsTotal = 1;
        //public int AttemptsPerformed;
        public string Result = Consts.FALSE;
        public string ResultMessage = "";
        public byte[] MessageContent = null;

        public List<MailLink> ListMailLink = new List<MailLink>();
    }

    public class MailRuleResult
    {
        public int Id = DM4Consts.DefaultNULLId;
        public string Rule = "";
        public MailRuleType RuleType = MailRuleType.Empty;
        public bool IsMatch;
        public string Result;
    }
}